//
//  UIView+RectSize.m
//  YuiKe
//
//  Created by yuike5 yuike5 on 11-11-3.
//  Copyright (c) 2011年 yuike. All rights reserved.
//
#import "header.h"
#import "UIView+RectSize.h"



@implementation UIView (RectSize)

+(UIEdgeInsets) autoAdjustRectx:(CGRect)rect toSize:(CGSize)size
{
    CGRect temp = [self autoAdjustRect:rect toSize:size];
    UIEdgeInsets edge;
    edge.left = temp.origin.x;
    edge.top = temp.origin.y;
    edge.right = rect.size.width - temp.origin.x - temp.size.width;
    edge.bottom = rect.size.height - temp.origin.y - temp.size.height;
    return edge;
}

+(CGRect) autoAdjustRect:(CGRect)frame toSize:(CGSize)imageSize
{
    CGSize frameSize = frame.size;
    if (imageSize.width == 0 || imageSize.height == 0)
        return frame;
    if (frameSize.width == 0 || frameSize.height == 0)
        return frame;
    
    
    if (imageSize.height / imageSize.width > frameSize.height / frameSize.width) {
        // image size tooooo height
        if (imageSize.height > frameSize.height) {
            float p = frameSize.height / imageSize.height;
            imageSize.height *= p;
            imageSize.width *= p;
        }
    } else {
        // image size tooooo width
        if (imageSize.width > frameSize.width) {
            float p = frameSize.width / imageSize.width;
            imageSize.height *= p;
            imageSize.width *= p;
        }
    }
    frame.size = imageSize;
    frame.origin.x += (frameSize.width - imageSize.width) / 2;
    frame.origin.y += (frameSize.height - imageSize.height) / 2;
    return IntxFrame(frame);
}

// 把size缩放到刚好放到 rect 里面
+(CGRect) autoAdjustRect:(CGRect)rect scaleSize:(CGSize)size lastCenter:(CGPoint)center
{
    if (rect.size.width == 0 || rect.size.height == 0)
        return rect;
    if (size.width == 0 || size.height == 0)
        return rect;
    

    CGRect centerRect = [UIView autoAdjustRect:rect toSize:size];
    float px = MAX(centerRect.size.width / rect.size.width, 
                   centerRect.size.height / rect.size.height);
    float xaddr = (centerRect.size.width / px - centerRect.size.width) / 2;
    float yaddr = (centerRect.size.height / px - centerRect.size.height) / 2;
    
    centerRect.origin.x -= xaddr / 2;
    centerRect.origin.y -= yaddr / 2;
    centerRect.size.width += xaddr;
    centerRect.size.height += yaddr;
    
    CGPoint curCenter = CGPointMake(centerRect.origin.x+centerRect.size.width/2,
                                    centerRect.origin.y+centerRect.size.height/2);
    centerRect.origin.x += center.x - curCenter.x;
    centerRect.origin.y += center.y - curCenter.y;
    return IntxFrame(centerRect);
}



// 太小，顶上居中对齐；太大，等比缩放，宽适应。。。
+(CGRect) autoAdjustRectDisp:(CGRect)bounds toSize:(CGSize)imageSize
{
    CGSize boundsSize = bounds.size;
    if (boundsSize.width == 0 || boundsSize.height == 0)
        return IntxFrame(bounds);
    if (imageSize.width == 0 || imageSize.height == 0)
        return IntxFrame(bounds);

    
    if (boundsSize.width >= imageSize.width) {
        // 顶上，居中对齐...
        CGRect frame = bounds;
        frame.origin.x += (boundsSize.width - imageSize.width) / 2;
        frame.size = imageSize;
        return IntxFrame(frame);
        
    } else {
        // 横向拉伸对齐
        CGRect frame = bounds;
        frame.size.width = boundsSize.width;
        frame.size.height = imageSize.height * boundsSize.width / imageSize.width;
        return IntxFrame(frame);
    }
}



-(CGPoint) boundsCenter
{
    CGPoint center = self.bounds.origin;
    center.x += self.bounds.size.width/2;
    center.y += self.bounds.size.height/2;
    return IntxPoint(center);
}
-(void) removeSubviews
{
    for (UIView* view in self.subviews) {
        [view removeFromSuperview];
    }
}
-(void) removeSubviews:(Class)clasz
{
    for (UIView* view in self.subviews) {
        if ([view isKindOfClass:clasz]) {
            [view removeFromSuperview];
        }
    }
}

+(BOOL) setTreeCurrent:(UIView*)curView_ alpha:(float)alpha_ without:(UIView*)view_
{
    if (curView_ == view_) return YES;
    BOOL childFound = NO;
    for (UIView* view in [curView_ subviews]) {
        BOOL temp = [UIView setTreeCurrent:view alpha:alpha_ without:view_];
        if (temp)
            childFound = YES;
    }
    if (!childFound)
        curView_.alpha = alpha_;
    return childFound;
}


- (CGRect) frameInAncestorView: (UIView*) parentx
{
    UIView* this = self;
    
    CGRect retf = this.frame;
    while (this.superview!=nil && this.superview!=parentx) {
        retf.origin.x += this.superview.frame.origin.x;
        retf.origin.y += this.superview.frame.origin.y;
        
        if ([this.superview isKindOfClass:[UIScrollView class]]) {
            UIScrollView* scroll = (UIScrollView*) this.superview;
            retf.origin.x -= scroll.contentOffset.x;
            retf.origin.y -= scroll.contentOffset.y;
        }
        
        this = this.superview;
    }
    return retf;
}
@end
